Python Basics

code
data-analysis
Author

Jake Starkey

Published

February 18, 2024

Variables Are Names, Not Places

*A value is datum (literal) such as a number or text

*There are different types of values:

*352.3 is known as a float or double;

*22 is an integer;

*“Hello World!” is a string.

list_example =  [10, 1.23, "like this", True, None]
print(list_example)
type(list_example)
[10, 1.23, 'like this', True, None]
list

[10, 1.23, ‘like this’, True, None]

*The most. basic built-in data types that we’ll need to know about are: integers, 10 floats, 1.23 strings, “like this” booleans, True nothing, None

*Python also has a built-in type of data container called a list (ex. [10,15,20]) that can contain anything, even different types

##Values, Variables, and Types

a = 10
print(a)
10

*A variable is a name that refers to a value.

**We can think of a variable as a box that has a value, or multiple values, packed inside it A variable is just a name!

*Sometimes you will hear variables referred to as objects.

*Everything that is not a literal value, such as 10, is an object

##Assignment (=)

# Here we assign the integer value 5 to the variable x.
x = 5   

# Now we can use the variable x in the next line.
y = x + 12  
y
17

*In Python, we use = to assign a value to a variable

*In math, = means equality of both sides

*In programs, = means assignment: assign the value on the right side to the variable on the left side.

*In programming code, everything on the right side needs to have a value.

*The right side can be a literal value, or a variable that has already been assigned a value, or a combination.

*When Python reads y= x + 12, it does the following: Sees the = in the middle.Knows that this is an assignment.

*Calculates the right side (gets the value of the object referred to by x and adds it to 12).

*Assigns the result to the left-side variable, y.

##Code and Comment Style

*The two main principles for coding and managing data are: Make things easier for your future self. Don’t trust your future self.

*The # mark is Google Colab’s comment character The # character has many names: hash, sharp, pound, or octothorpe. The # indicates that the rest of the line is to be ignored. **Write comments before the line that you want the comment to apply to.

*Consider adding more comments on code cells and their results using text cells.

##Brackets *There are several kinds of brackets in Python, including [], {}, and ().

vector = ['a', 'b']
vector[0]
'a'

*[] is used to denote a list or to signify accessing a position using an index

{'a', 'b'}  # set
{'first_letter': 'a', 'second_letter': 'b'}  # dictionary
{'first_letter': 'a', 'second_letter': 'b'}

{‘first_letter’: ‘a’, ‘second_letter’: ‘b’} {} is used to denote a set or a dictionary (with key-value pairs)

*() is used to denote a tuple, or the arguments to a function, ex. function(x) where x is the input passed to the function ##Q1

(2**5/(7*(4-2**3)))
-1.1428571428571428

##Q2

20 == '20'
False

*This is saying 20 is not equal to ‘20’ because they are different data types (int vs string)

x = 4.0
y = .5
z = 3*y - x

x < y or 3*y < x
True

True This says the expression is true since 3.5 < 4

##Q3

fare = "$10.00"
tip = "2.00$"
tax = "$ 0.80"

Write a Python code that uses slicing and the print() function to print out the following message:

The total trip cost is: $12.80

total =fare = "$10.00"
tip = "2.00$"
tax = "$ 0.80"
total = fare[0:2] + tip[0] + tax[3:6]
print("The total trip cost is:", total)

#The total trip cost is: $12.80The total trip cost is:", total)
The total trip cost is: $12.80

##Q4

list_variable = [100, 144, 169, 1000, 8]

Write a Python code that uses print() and max() functions to print out the largest value in the list, list_variable, as follows:

The largest value in the list is: 1000

list_variable = [100,144,169,1000,8]
x =max(list_variable)
print('The largest value in the list is:',x)

#The largest value in the list is: 1000
The largest value in the list is: 1000

The largest value in the list is: 1000 ##Q5 Import the pandas library as pd. Install the itables package. *From itables, import the function init_notebook_mode and show.

import pandas as pd
!pip install itables
from itables import init_notebook_mode
from itables import show
Requirement already satisfied: itables in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (1.6.3)
Requirement already satisfied: IPython in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from itables) (8.15.0)
Requirement already satisfied: pandas in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from itables) (2.0.3)
Requirement already satisfied: numpy in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from itables) (1.24.3)
Requirement already satisfied: backcall in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (0.2.0)
Requirement already satisfied: decorator in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (0.18.1)
Requirement already satisfied: matplotlib-inline in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (0.1.6)
Requirement already satisfied: pickleshare in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (0.7.5)
Requirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (3.0.36)
Requirement already satisfied: pygments>=2.4.0 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (2.15.1)
Requirement already satisfied: stack-data in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (0.2.0)
Requirement already satisfied: traitlets>=5 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (5.7.1)
Requirement already satisfied: pexpect>4.3 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (4.8.0)
Requirement already satisfied: appnope in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from IPython->itables) (0.1.2)
Requirement already satisfied: python-dateutil>=2.8.2 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from pandas->itables) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from pandas->itables) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from pandas->itables) (2023.3)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from jedi>=0.16->IPython->itables) (0.8.3)
Requirement already satisfied: ptyprocess>=0.5 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from pexpect>4.3->IPython->itables) (0.7.0)
Requirement already satisfied: wcwidth in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->IPython->itables) (0.2.5)
Requirement already satisfied: six>=1.5 in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas->itables) (1.16.0)
Requirement already satisfied: executing in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from stack-data->IPython->itables) (0.8.3)
Requirement already satisfied: asttokens in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from stack-data->IPython->itables) (2.0.5)
Requirement already satisfied: pure-eval in /Users/jakestarkey/anaconda3/lib/python3.11/site-packages (from stack-data->IPython->itables) (0.2.2)